<?php

namespace app\api\controller\product;

use app\api\model\product\Category as CategoryModel;
use app\api\controller\Controller;
use app\common\model\page\PageCategory as PageCategoryModel;
use app\api\model\order\Order as OrderModel;
use app\Request;
use think\facade\Cache;

/**
 * 商品分类控制器
 */
class Category extends Controller
{
    /**
     * 分类页面
     */
    public function index()
    {

        $user = $this->getUser();
        // 代理城市设置
        // $cityInfo = $user ? Cache::get($user['user_id'] . '_mycity_numid') : [];
        // $city_id = $cityInfo[1]??0;
        $city_id = 0;
        //首单数据
        $OrderModel = new OrderModel;
        $firstOrder = $OrderModel->getCount($user, 'all',true);
        $firstYN = ($firstOrder>0)?1:0; //会员：2全部，1普通会员，0新人
        // 分类模板
        $template = PageCategoryModel::detail();

        // 20240513 Mr.Z 调整为商户
        $user = $this->getUser();
        $Shopinfo = $this->getUserCache();
        $list = CategoryModel::getSupplierCate([],$firstYN,$Shopinfo['shop'])['tree'];
        // 商品分类列表
        // $list = array_values(CategoryModel::getCacheTree($firstYN));
        // 20240513 Mr.Z 去掉代理商设置
        // if($city_id > 0){
        //     foreach ($list as $k => $v){
        //         $list[$k]['child'] = CategoryModel::getProxyCate($v['child'],$city_id);
        //     }
        // }

        return $this->renderSuccess('', compact('template','list'));
    }

    public function productindex(Request $request){
        $params = $request->param();
        $parentId = $params['category_id']??0;
        $user = $this->getUser();
        // 代理城市设置
        // $cityInfo = $user ? Cache::get($user['user_id'] . '_mycity_numid') : [];
        // $city_id = $cityInfo[1]??0;
        $user = $this->getUser();
        $Shopinfo = $this->getUserCache();
        $list = (new CategoryModel)->getProcuteList($parentId,$user,$Shopinfo['shop']);
        return $this->renderSuccess('', compact('list'));
    }


}

